package cn.livingCloud.service.impl;

import cn.livingCloud.dto.*;
import cn.livingCloud.entity.core.PushGroup;
import cn.livingCloud.entity.core.PushGroupMember;
import cn.livingCloud.entity.core.ReportRule;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.repository.PushGroupMapper;
import cn.livingCloud.repository.PushGroupMemberMapper;
import cn.livingCloud.repository.ReportRuleMapper;
import cn.livingCloud.service.PushGroupService;
import cn.livingCloud.service.impl.operation.PushGroupFormDtoUpdater;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DingJie on 2016/6/21.
 */
@Service
public class PushGroupServiceImpl implements PushGroupService {
    @Autowired
    private PushGroupMapper mapper;
    @Autowired
    private PushGroupMemberMapper pushGroupMemberMapper;
    @Autowired
    private ReportRuleMapper reportRuleMapper;


    @Override
    public void loadPushGroupListDto(PushGroupListDto listDto) {
        List<ReportRule> listReportRule = reportRuleMapper.getListReportRule();
        List<ReportRuleDto> listReportRuleDto = new ArrayList<>();
        for (ReportRule reportRule : listReportRule) {
            listReportRuleDto.add(new ReportRuleDto(reportRule));
        }
        List<PushGroup> list = mapper.getList(listDto);
        listDto.setAllReportRuleDto(listReportRuleDto);
        listDto.addAllList(list);
    }

    @Override
    public PushGroupFormDto loadPushGroupFormDto(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            List<ReportRule> listReportRule = reportRuleMapper.getListReportRule();
            return new PushGroupFormDto(listReportRule);
        } else {
            PushGroup pushGroup = mapper.selectByPrimaryKey(uuid);
            List<ReportRule> listReportRule = reportRuleMapper.getListReportRule();
            return new PushGroupFormDto(pushGroup, listReportRule);
        }
    }

    @Override
    public void edit(PushGroupFormDto formDto) {
        PushGroupFormDtoUpdater updater = new PushGroupFormDtoUpdater(formDto);
        updater.update();
    }

    @Override
    public void delete(String uuid) {
        PushGroup pushGroup = mapper.selectByPrimaryKey(uuid);
        pushGroup.setArchive(true);
        mapper.updateByPrimaryKey(pushGroup);
        //删除分组的情况下解绑分组下的粉丝
        List<PushGroupMember> byMemberUuid = pushGroupMemberMapper.findByMemberUuid(uuid);
        for (PushGroupMember pushGroupMember : byMemberUuid) {
            pushGroupMember.setArchive(true);
            pushGroupMemberMapper.updateByPrimaryKey(pushGroupMember);
        }
    }

    @Override
    public PushGroupDto uploadState(String uuid, boolean state) {
        PushGroup pushGroup = mapper.selectByPrimaryKey(uuid);
        //如果前台传入执行的状态和数据库的状态相同那么就抛出异常
        if (state == pushGroup.isState()) {
            throw new IllegalStateException("禁用/启用失败,请检查状态是否被禁用/启用!");
        }
        //如果前台的状态和数据库的状态不相同那么就执行当前传入的操作
        pushGroup.setState(state);
        mapper.updateByPrimaryKey(pushGroup);
        return new PushGroupDto(pushGroup);
    }

    @Override
    public PushGroupBindMemberDto loadBindMemberDto(String pushGroupUuid) {
        return new PushGroupBindMemberDto(pushGroupUuid);
    }

    @Override
    public void loadMemberByPushGroupUuidListDto(MemberByPushGroupUuidListDto listDto) {
        List<Member> list = new ArrayList<>();
        List<PushGroupMember> listByPushGroupUuid = pushGroupMemberMapper.getListByPushGroupUuid(listDto);
        for (PushGroupMember pushGroupMember : listByPushGroupUuid) {
            list.add(pushGroupMember.getMember());
        }
        listDto.addAllList(list);
    }

    @Override
    public void bindMember(PushGroupBindMemberDto formDto) {
        PushGroupMember pushGroupMember = new PushGroupMember();
        pushGroupMember.setPushGroupUuid(formDto.getPushGroupUuid());
        pushGroupMember.setMemberUuid(formDto.getMemberUuid());
        pushGroupMemberMapper.insert(pushGroupMember);
    }

}
